package com.hzy.fivepalace.admin.controller.base;

import com.hzy.fivepalace.common.consts.CommConsts;
import com.hzy.fivepalace.db.domain.DtsAdmin;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public abstract class BaseController {


    /**
     * 判断后台管理-用户信息是否是admin
     *
     * @return
     */
    protected boolean sysIsAdmin() {
        DtsAdmin user = getAdminInfo();
        return user.getUsername().equals("admin") || user.getId().intValue() == 1;
    }


    /**
     * 获取当前管理员id
     *
     * @param request
     * @return
     */
    public Integer getAdminId(HttpServletRequest request) {
        return NumberUtils.toInt(request.getAttribute(CommConsts.ADMIN_Id_KEY).toString());
    }

    /**
     * 获取当前管理员id
     *
     * @return
     */
    public Integer getAdminId() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        return NumberUtils.toInt(request.getAttribute(CommConsts.ADMIN_Id_KEY).toString());
    }

    /**
     * 获取当前管理员token
     * @param request
     * @return
     */
    public String getAdminToken(HttpServletRequest request) {
        return request.getAttribute(CommConsts.ADMIN_TOKEN_KEY).toString();
    }

    /**
     * 获取当前管理员token
     *
     * @return
     */
    public String getAdminToken() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        return request.getAttribute(CommConsts.ADMIN_TOKEN_KEY).toString();
    }

    /**
     * 获取当前管理员信息
     *
     * @param request
     * @return
     */
    public DtsAdmin getAdminInfo(HttpServletRequest request) {
        return (DtsAdmin) request.getAttribute(CommConsts.ADMIN_INFO_KEY);
    }

    /**
     * 获取当前管理员信息
     *
     * @return
     */
    public DtsAdmin getAdminInfo() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        return (DtsAdmin) request.getAttribute(CommConsts.ADMIN_INFO_KEY);
    }
}
